<?php
namespace Mobile\Controller;
use Mobile\Model\goodsModel;
use Mobile\Model\goodstypeModel;
use Mobile\Model\focusModel;

class IndexController extends BaseController {
    
    public $goodsModel;
    public $goodstypeModel;
    
    public function __construct() {
        parent::__construct();
        $this->focusModel = new focusModel();
        $this->goodsModel = new goodsModel();
        $this->goodstypeModel = new goodstypeModel();
		
        $BindController = new BindController();
        $BindController->autobind();
    }

    /* 绑定上级 */
    public function bindrefer () {
        if(IS_AJAX) {
            $do = get('do');
            if($do=='refer') {
                $uid = get('uid');
                if(!$uid) {
                    return_json(1,'');
                }
                $refer = $this->memberModel->where(array('uid'=>$uid))->find();
                if(!$refer) {
                    return_json(1,'');
                }
                $last = $refer;
                while($last) {
                    if($last['refer_uid']==$this->memberinfo['uid']) {
                        return_json(1,'');
                    }
                    $last = $this->memberModel->where(array('uid'=>$last['refer_uid']))->find();
                }
                $info = array(
                    'uid' => $uid,
                    'headimgurl' => $refer['headimgurl'],
                    'nickname' => $refer['nickname']?$refer['nickname']:$refer['realname']
                );
                return_json(0,'',$refer);
            }elseif($do=='bind') {
                $uid = get('uid');
                if(!$uid) {
                    return_json(1,'推荐人不存在');
                }
                $refer = $this->memberModel->where(array('uid'=>$uid))->find();
                if(!$refer) {
                    return_json(1,'推荐人不存在');
                }
                $last = $refer;
                while($last) {
                    if($last['refer_uid']==$this->memberinfo['uid']) {
                        return_json(1,'推荐人不存在');
                    }
                    if($last['refer_uid']==0) {
                        break;
                    }
                    $last = $this->memberModel->where(array('uid'=>$last['refer_uid']))->find();
                }
                $save = array(
                    'refer_uid' => $refer['uid'],
                    'refer_nickname' => $refer['nickname']
                );
                $res = $this->memberModel->where(array('uid'=>$this->uid))->save($save);
                if($res) {
                    return_json(0,'绑定成功','','/mobile/index');
                }else{
                    return_json(1,'绑定失败,请重试');
                }
            }

        }
        $seo_data = array(
            "title" => '绑定推荐人',
            "description" => "",
            "keywords" => "",
        );
        layout('Common/layout-vue');
        $this->setSeo($seo_data);
        $this->display();
    }
    /*
     * 获取商品分类
     */
    public function _get_gtype(){
        $data = $this->goodstypeModel->getGoodstypeList();
        $gtypes = array();
        
        foreach($data as $k=>$v){
            $gtypes[$v['gtype_id']] = $v;
        }
        $this->assign('gtype',$gtypes);
        return $data;
    }

    public function test(){
        dump($this->memberinfo);die;
    }
    
    
    public function index(){
        
        $title = C("SITE_NAME");

        $seo_data = array(
            "title" => $title,
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        
        //商品数量统计
        $goods_count = S("goods_count");

        if(!$goods_count){
            $goodsController=new GoodsController();
            $goods_count = $goodsController->countGoods();//商品
            S("goods_count",$goods_count,1*3600);
        }
        $this->assign('goods_count',$goods_count);
        
        //获取商品列表
        //$where = "ischeck=1 AND stock>0 and (goods_id!=2 and goods_id!=3)";
        $where = "ischeck=1 AND (goods_id!=2 and goods_id!=3)";
        $goods_list = $this->goodsModel->getGoodsList($where);

        //等级价格
        $grade_id = $this->memberinfo['grade_id'];
        $grade_id = $grade_id ? $grade_id : 1;
        foreach($goods_list as $k=>$v){
            $goods_list[$k]['shop_price'] = $v['grade'.$grade_id.'_price'];
        }

        $this->assign('datalist',$goods_list);
        $this->assign("grade",$this->memberinfo['grade_id']);
        $share_data = array(
            "title" => C("SITE_NAME"),
        );
        $this->setShare($share_data);
        
        
        if($this->is_wechat) {
            layout('Common/layout-vue');
            $this->display('new');
        }else{
            layout('Common/layout-vue');
            $this->display('new');
        }
        
    }

    public function check () {
        $seo = array(
            'title' => '绑定手机'
        );
        if(IS_AJAX) {
            $data = $_POST;
            $res = checksmscode($data['mobile'],$data['code']);
            if(!$res) {
                return_json(1,'验证码错误');
            }
            $save = array(
                'mobile' => $data['mobile']
            );
            $this->memberModel->where(array('uid'=>$this->uid))->save($save);
            return_json(0,'绑定成功','','/mobile/index');
        }
        $adStatus = C('AD_STATUS');
        $adImgUrl = C('AD_IMG_URL');
        
        if($adImgUrl && $adStatus==1) {
            $this->assign('showad',1);
        }
        $this->setSeo($seo);
        $this->display();
    }

    public  function sendsms () {
        $mobile = get('mobile');
        if(!check_mobile($mobile)) {
            //return_json(1,'手机号格式错误');
        }
        $old = $this->memberModel->where(array('mobile'=>$mobile))->find();
        if($old) {
            return_json(1,'手机号已被占用，请更换手机号');
        }
        $res = sendsmscode($mobile);
        if($res) {
            return_json(0,'发送成功');
        }else{
            return_json(1,'发送失败');
        }
    }

    public function apiInitIndex () {
        /* 分类列表 */
        $typeList = M('goodstype')->field('gtype_name as text,gtype_id as type')->order('gtype_ord asc')->select();
        /* 首页轮播 */
        $where = array(
            "status" => 1,
            "position" => 1
        );
        $focus_list = $this->focusModel->getFocusLists($where);
        foreach($focus_list as $k=>$v) {
            $focus_list[$k]['pic'] = C('UPYUN_DOMAIN').$v['pic'];
        }
        /* 本月新品 */
        $field1 = "concat('".C('UPYUN_DOMAIN')."',month_imgurl) as imgurl,concat('/mobile/goods?goods_id=',goods_id) as url";
        $field2 = "*,concat('".C('UPYUN_DOMAIN')."',hot_imgurl) as imgurl,concat('/mobile/goods?goods_id=',goods_id) as url".
        ",goods_name as name";
        
        $monehList = M('goods')->where(array(
            'is_new' => 1,
            'ischeck' => 1,
            'stock' => array('gt',0),
            '_string' => "(month_imgurl is not null && month_imgurl != '')"
        ))->field($field1)->order('ord asc')->select();
        /* 热卖单品 */
        $hotList = M('goods')->where(array(
            'is_hot' => 1,
            'ischeck' => 1,
            'stock' => array('gt',0),
            '_string' => "(hot_imgurl is not null && hot_imgurl != '')"
        ))->field($field2)->order('ord asc')->page(1,3)->select();
        $hoteNew = array();
        $grade = max($this->memberinfo['grade_id'],1);
        foreach($hotList as $k=>$v) {
            $hoteNew[] = array(
                'img' => $v['imgurl'],
                'url' => $v['url'],
                'name' => $v['name'],
                'url' => $v['url'],
                'price' => $v['grade'.$grade.'_price'],
                'market_pri' => $v['market_price']
            );
        }
        $data = array(
            'swiperList' => $focus_list,
            'monthList' => $monehList,
            'hotList' => $hoteNew,
            'typeList' => $typeList
        );
        return_json(0,'',$data);
    }

    /*  */
    public function apiGetGoodsList () {
        $type = get('type');
        $key = get('key');
        $field = "*,concat('".C('UPYUN_DOMAIN')."',goods_pic) as img,concat('/mobile/goods?goods_id=',goods_id) as url"
        .",goods_name,sale_all as buy_num,market_price as market_pri";
        $where = array(
            'ischeck' => 1,
            'stock' => array('gt',0),
        );
        if($type) {
            $where['gtype_id'] = $type;
        }elseif($key) {
            $where['goods_name'] = array('like','%'.$key.'%');
        }
        $list = M('goods')->where($where)->field($field)->order('ord asc')->select();
        if(!$list) {
            return_json(1,'');
        }
        $new = array();
        $grade = max($this->memberinfo['grade_id'],1);
        foreach($list as $k=>$v) {
            $new[] = array(
                'img' => $v['img'],
                'url' => $v['url'],
                'goods_name' => $v['goods_name'],
                'desc1' => $v['desc1'],
                'desc2' => $v['desc2'],
                'buy_num' => $v['buy_num'],
                'url' => $v['url'],
                'price' => $v['grade'.$grade.'_price'],
                'market_pri' => $v['market_price']
            );
        }
        if($new) {
            return_json(0,'',$new);
        }else{
            return_json(1,'');
        }
    }

    public function makemoney () {
        $seo = array(
            'title' => '如何赚钱'
        );
        $this->setSeo($seo);
        $this->display();
    }
    
}